package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: Zhangweichuang
 * @date: 2020/10/28-19:18
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Reference
    RoleService roleService;


    /**
     * 查找所有权限
     *
     */
    @RequestMapping("/findAllRolePermission")
    public Result findAllRolePermission(){
        log.debug("查找所有角色权限！！");
        List <Permission> roleList=roleService.findAllRolePermission();
        log.debug("所有的权限集合！！"+roleList);
        return new Result(true,"添加用户成功",roleList);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionsIds){
        log.debug("!!!!!checkItem!!!!!"+role);
        log.debug("!!!!!checkItem!!!!!"+permissionsIds);
        roleService.add(role,permissionsIds);
        log.debug("数据新建成功！！！");
        return new Result(true, "添加用户成功");
    }

    /**
     * 分页数据显示
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("queryPageBean"+queryPageBean);
        PageResult pageResult=roleService.findPage(queryPageBean);
        log.debug("pageResult"+pageResult);
        return new Result(true,"角色信息查询成功",pageResult);
    }
    /**
     * 回显角色的基本数据
     */
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id){
        Role role=roleService.findRoleById(id);
        return new Result(true,"角色基本数据回显成功！",role);
    }
    /**
     * 回显角色所关联的权限
     */
    @RequestMapping("/findPermissionById")
    public Result findPermissionById(Integer id){
        List<Integer> permission = roleService.findPermissionById(id);

        return new Result(true,"回显角色所关联的权限回显成功！",permission);
    }

    /**
     * 编辑数据
     * @param permissionsIds
     * @param role
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(Integer[] permissionsIds,@RequestBody Role role){
        roleService.edit(role,permissionsIds);
        return new Result(true,"编辑数据成功！！");
    }
    /**
     * 删除
     */
    @RequestMapping("/delById")
    public Result delById(Integer id){
        roleService.delById(id);
        return new Result(true,"删除成功！！");
    }

}
